/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ddsunase_parser;

/**
 *
 * @author samy
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.TreeMap;

/**
 *
 * @author samy
 */
public class SortReaderObj {

    private TreeMap<Integer, SortObj> mapResults = new TreeMap<Integer, SortObj>();

    public SortReaderObj(String filePath) {

        File file = new File(filePath);
        readMapFromCSV(file);

    }

    public void readMapFromCSV(File file) {

        mapResults.clear();
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        SortObj deo;

        try {

            br = new BufferedReader(new FileReader(file));

            boolean firstTime = true;

            while ((line = br.readLine()) != null) {

                String[] values = line.split(cvsSplitBy);

                if (!firstTime) {

                    if (mapResults.get(Integer.parseInt(values[0])) == null) {
                        deo = new SortObj();
                        deo.setBugID(Integer.parseInt(values[0]));

                        if (values[1] != null && values[1].trim().length() > 0) {
                            try {
                                deo.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(values[1]));
                            } catch (Throwable t) {
                                //    System.out.println("Error parsing date at "+values[1]);
                            }
                        }
                        if (values.length > 2) {

                            if (values[2] != null && values[2].trim().length() > 0) {
                                try {
                                    deo.setResolutionsDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(values[2]));
                                } catch (Throwable t) {
                                    //        System.out.println("Error parsing date at "+values[2] );
                                }
                            }
                        }

                        if (values.length > 2) {

//                        System.out.println("Size of "+values.length);
                            ///     if (values[6] != null && values[6].trim().length() > 0) {
                            try {
                                deo.setAnyResoDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(values[6]));

                            } catch (Throwable t) {
                                //      System.out.println("Error parsing date at "+values[6] );
                            }
                            //     }

                        }
                    } else {
                        deo = mapResults.get(Integer.parseInt(values[0]));
                    }

//                    deo.setProductId(values[4]);
//                    deo.setCompoenetId(values[5]);
//                    deo.setReportId(values[9].trim());
//
//                    if (values.length > 2) {
//
////                        System.out.println("Size of "+values.length);
//                        ///     if (values[6] != null && values[6].trim().length() > 0) {
//                        try {
//                            deo.addElementActivity(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(values[6]), values[7], values[8]);
//                        } catch (Throwable t) {
//                            //      System.out.println("Error parsing date at "+values[6] );
//                        }
//                        //     }
//
//                    }

                    mapResults.put(deo.getBugID(), deo);

                }

                firstTime = false;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public TreeMap<Integer, SortObj> getMapResults() {
        return mapResults;
    }

    public void setMapResults(TreeMap<Integer, SortObj> mapResults) {
        this.mapResults = mapResults;
    }

}
